source config.sh

if [ "$1" = "" ]; then
	echo please input n
	exit
fi
if [ "$2" = "" ]; then
	echo please input server ID
	exit
fi

ServerID="$2"

for (( c=1; c<=$1; c++ ))
do
	echo "$Main: interation# $c"
	echo "$Main: generating Instance User File"
	$GenerateInstanceUser
	
	while read line
	do
		user=$(echo $line | awk {'print $1'})
		IFS='
		'
		array_a=( $($Commander $user $GetAllRelatedInstances $user $ServerID) )
		numOfInstances=$((${#array_a[@]}-2))
		echo "$Main: num of related instances of $user to $ServerID is $numOfInstances"
		if [ $numOfInstances -gt 0 ];then	
			echo "$Main: after 10 seconds all related instances of $user to $ServerID will be rebooted/started"
			sleep 5
			($Commander $user $RebootOrStartRelatedInstances $user $ServerID)&
			sleep 5
		fi
	done < $UserFileName
	
	ServerUser=$(cat $InstanceUserFile | grep "$ServerID" | awk {'print $2'})
	
	echo "$Main: after 80 seconds the server will be executed..."
	sleep 80
	($Commander $ServerUser $ExecuteServer $ServerID)&
	echo "$Main: after 30 seconds the related clients will be executed"
	sleep 30
	
	
	while read line
	do
		user=$(echo $line | awk {'print $1'})
		IFS='
		'
		array_a=( $($Commander $user $GetAllRelatedInstances $user $ServerID) )
		numOfInstances=$((${#array_a[@]}-2))
		echo "$Main: num of related instances of $user to $ServerID is $numOfInstances"
		if [ $numOfInstances -gt 0 ];then
			echo "$Main: after 60 seconds $numOfInstances clients of $user will be executed"
			sleep 30
			($Commander $user $ExecuteAllRelatedClients $user $ServerID)&
			sleep 30
		fi
	done < $UserFileName

	echo "$Main: after 20 seconds we start observing the server"
	sleep 20 
	$Commander $ServerUser $CheckOutOnServer $ServerID
done

exit


		


